package com.yungu.swift.system.sys.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.system.sys.dao.SysAccountAgentMapper;
import com.yungu.swift.system.sys.model.dto.SysAccountAgentDto;
import com.yungu.swift.system.finance.model.enums.FinanceEnum;
import com.yungu.swift.system.finance.model.param.AdminAccountFlowPageParam;
import com.yungu.swift.system.finance.model.param.AdminAccountPageParam;
import com.yungu.swift.system.sys.model.vo.SysAgentAccountFlowVo;
import com.yungu.swift.system.sys.model.vo.SysAgentAccountVo;
import com.yungu.swift.system.sys.service.SysAccountAgentService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;

@Service
public class SysAccountAgentServiceImpl extends BaseServiceImpl<SysAccountAgentDto> implements SysAccountAgentService {
    @Autowired
    private SysAccountAgentMapper sysAccountAgentMapper;

    @Override
    protected IMapper<SysAccountAgentDto> getMapper() {
        return sysAccountAgentMapper;
    }

    @Override
    public ResponseData<PageVo<SysAgentAccountVo>> queryPageVo(AdminAccountPageParam accountPageParam) {
        PageBounds pageBounds = new PageBounds(accountPageParam.getPageNum(), accountPageParam.getPageSize());
        PageList<SysAgentAccountVo> pageList = sysAccountAgentMapper.queryPageVo(accountPageParam, pageBounds);
        PageVo<SysAgentAccountVo> pageVo = new PageVo(accountPageParam.getPageNum(), accountPageParam.getPageSize(),
                pageList.getPaginator().getTotalCount());
        pageVo.setItems(pageList);
        return ResponseData.buildSuccessResponse(pageVo);
    }

    @Override
    public ResponseData<List<SysAgentAccountVo>> export(AdminAccountPageParam accountPageParam) {
        return ResponseData.buildSuccessResponse(sysAccountAgentMapper.queryPageVo(accountPageParam));
    }

    @Override
    public ResponseData<SysAgentAccountVo> statistics(AdminAccountPageParam accountPageParam) {
        return ResponseData.buildSuccessResponse(sysAccountAgentMapper.statistics(accountPageParam));
    }

    @Override
    public ResponseData<PageVo<SysAgentAccountFlowVo>> flowQueryPage(AdminAccountFlowPageParam flowPageParam) {
        PageBounds pageBounds = new PageBounds(flowPageParam.getPageNum(), flowPageParam.getPageSize());
        PageList<SysAgentAccountFlowVo> pageList = sysAccountAgentMapper.flowQueryPage(flowPageParam, pageBounds);
        PageVo<SysAgentAccountFlowVo> pageVo = new PageVo(flowPageParam.getPageNum(), flowPageParam.getPageSize(),
                pageList.getPaginator().getTotalCount());
        fillDate(pageList, flowPageParam.getUuid());
        pageVo.setItems(pageList);
        return ResponseData.buildSuccessResponse(pageVo);
    }

    @Override
    public ResponseData<List<SysAgentAccountFlowVo>> flowExport(AdminAccountFlowPageParam flowPageParam) {
        List<SysAgentAccountFlowVo> accountFlowVos = sysAccountAgentMapper.flowQueryPage(flowPageParam);
        fillDate(accountFlowVos, flowPageParam.getUuid());
        return ResponseData.buildSuccessResponse(accountFlowVos);
    }

    /**
     * 补充数据含义
     *
     * @param list
     * @param uuid
     */
    private void fillDate(List<SysAgentAccountFlowVo> list, String uuid) {
        list.stream().forEach(accountFlowVo -> {
            accountFlowVo.setCourseDescription(FinanceEnum.getMsg(accountFlowVo.getCourse()));
            accountFlowVo.setTransactionType(Objects.equals(accountFlowVo.getFinanceInflow(), uuid) ? "收入" : "支出");
        });
    }
}